#include "fileutil.h"
#include <QFile>
#include <QTextStream>
#include <iostream>
#include <QtDebug>
Vocabulary FileUtil::loadVocabularyInfo(const QString &fileName)
{
//    Vocabulary vocabulary;
//    QFile aFile(fileName);
//    if (!aFile.exists()) //文件不存在
//    {
//        std::cout << "file not exist" << std::endl;;
//        return vocabulary;
//    }
//    if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
//    {
//        std::cout << "file can not open" << std::endl;
//        return vocabulary;
//    }
}

QList<Word> FileUtil::loadWordsFormFile(const QString &fileName)
{

    QList<Word> words;
    //以读模式打开文件
    QFile aFile(fileName);
    if (!aFile.exists()) //文件不存在
    {
        std::cout << "file not exist" << std::endl;;
        return words;
    }
    if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        std::cout << "file can not open" << std::endl;
        return words;
    }

//    QTextStream aStream(&aFile); //用文本流读取文件

    while (!aFile.atEnd())
    {

        Word word;
        QString original;
        QString translation;

        QString front;
        QString back;
        QString oneWord;
        QString aLine = aFile.readLine();
        aLine = aLine.trimmed();


        QStringList strList = aLine.split(" ");

        if(strList.length() != 0)
        {
            oneWord = strList[0];
        }
        front = oneWord;
        back = oneWord;

        for(int i = 1; i < strList.length(); i++)
        {
            oneWord = strList[i];
            front = back;
            back = oneWord;

            original += front;
        }
        translation = back;

        word.setOriginal(original);
        word.setTranslation(translation);


        words.push_back(word);
    }
    aFile.close();

    return words;
}
